/**
 * http 请求，回调对象
 * @param {string} uri        请求地址
 * @param {object} params     url参数
 * @param {object} headers    请求头
 * @param {object} data       请求体数据
*/
class Http {
  get({ uri, params = {}, headers = {} }) {
    return this.handle({ method: 'get', uri, params, headers })
  }
  post({ uri, params = {}, headers = {}, data = {} }) {
    return this.handle({ method: 'post', uri, params, headers, data })
  }
  handle({ method, uri, params, headers, data }) {
    params = formatObject(params);
    headers = formatObject(headers);
    var paramsMap = new Packages.java.util.HashMap();
    var headersMap = new Packages.java.util.HashMap();
    for (var i in params) {
      paramsMap.put(i, params[i].toString())
    }
    for (var j in headers) {
      headersMap.put(j, headers[j])
    }
    var response = null;
    if (method == 'get') {
      response = $.httpclient.sendGet(uri, paramsMap, headersMap);
    } else {
      var dataMap = $.json.objectToJsonString(data);
      response = $.httpclient.sendPost(uri, paramsMap, headersMap, dataMap);
    }
    var resData = JSON.parse(response);
    return resData;
  }
}
export var http = new Http();

/**
 * 发送站内信
 * @param {string} title   标题
 * @param {string} content 内容
 * @param {string} userId  接收的用户id
*/
export function sendMsg({ title = '站内信标题', content = '', userId = $.context.getCurrentUserId() }) {
  $.message.sendGeneralStationMessage(title, content, userId);
}

/**
 * 清空表单
 * @param {string} formName 表单名称
*/
export function clearDocuments(formName) {
  var applicationId = $.context.getCurrentApplicationId();
  $.form.deleteAllDocumentsByFormName(applicationId, formName);
}

/**
 * 导入表单
 * @param {string} formName 表单名称
 * @param {object} data     数据
*/
export function createDocument({ formName, formData }) {
  var applicationId = $.context.getCurrentApplicationId();
  var doc = $.form.createEmptyDocument(applicationId, formName);
  var data = formatObject(formData);
  for (var i in data) {
    doc.addElement(i, data[i].toString());
  }
  $.form.saveFormDocument(doc, applicationId);
}

/**
 * 修改表单数据
 * @param {string} document 表单数据
 * @param {object} data     数据
*/
export function reviseDocument({ document, formData }) {
  var applicationId = $.context.getCurrentApplicationId();
  var data = formatObject(formData);
  for (var i in data) {
    document.addElement(i, data[i].toString());
  }
  $.form.saveFormDocument(document, applicationId);
}

/**
 * 处理数据，把集合数据处理成普通js对象
 * @param {object} data 对象或集合
*/
export function formatObject(data) {
  if (typeof(data) == 'string') {
    return JSON.parse(string);
  }
  return JSON.parse($.json.objectToJsonString(data));
}

/**
 * 获取表单数据
 * 回调集合，非js对象
 * @param {string} formName  表单名称
 * @param {string} params    查询条件
*/
export function getDocuments({ formName, params = {} }) {
  var applicationId = $.context.getCurrentApplicationId();
  var  conditionMap = new Packages.java.util.HashMap();
  for (var i in params) {
    conditionMap.put(i, params[i].toString());
  }
  var documents = $.form.getDocumentsByCondition(applicationId, formName, conditionMap);
  return documents;
}

/**
 * 获取当前表单数据
 * 回调集合，非js对象
*/
export function getCurrentDocument() {
  return $.context.getCurrentDocument();
}

/**
 * 根据条件删除表单内容
 * @param {string} formName   表单名称
 * @param {object} conditions 删除条件
*/
export function deleteDocumentByCondition({formName, conditions}) {
  var applicationId = $.context.getCurrentApplicationId();
  var conditionMap = new Packages.java.util.HashMap();
  for (var i in conditions) {
    conditionMap.put(i, conditions[i].toString());
  }
  var delDataList = $.form.getDocumentsByCondition(applicationId, formName, conditionMap);
  var delIdList = new Packages.java.util.ArrayList();
  if (delDataList.size() > 0) {
    for (var i = 0; i < delDataList.size(); i++) {
      delIdList.add(delDataList.get(i).getId());
    }
    $.form.batchDeleteFormDocumentsByIdList(applicationId, formName, delIdList);
  }
}

/**
 * 时间字符串转Date对象
 * @param {string} timeStr 时间字符串，例如 '2021-11-11 11:11:11'
 * @param {string} format  格式字符串，例如 'yyyy-MM-dd hh:mm:ss'
*/
export function stringToDate(timeStr, format = 'yyyy-MM-dd hh:mm:ss') {
  return $.date.stringToDate(timeStr, format);
}

/**
 * 根据传入 时:分:秒 计算时间戳
 * @param {string} runTime '12:13:14'
*/
export function timeToStamp(runTime) {
  var nowTime = $.date.dateToString($.date.getCurrentDate(), 'hh:mm:ss');
  var runDateTimestamp = $.date.getCurrentDate().getTime();
  // 如果当前时间大于传入时间，往后加一天
  if (nowTime > runTime) {
    runDateTimestamp += 864e5;
  }
  var runTimeStr = $.date.dateToString($.date.timestampToDate(runDateTimestamp), 'yyyy-MM-dd ' + runTime);
  var timeStamp = $.date.stringToDate(runTimeStr, 'yyyy-MM-dd hh:mm:ss').getTime();
  return timeStamp;
}

/**
 * 获取当前时间
*/
export function getCurrentDate() {
  return $.date.getCurrentDate();
}
